package net.lucamasini.ricette.client.ricette;

import net.lucamasini.ricette.client.ingredienti.IngredienteProxy;
import net.lucamasini.ricette.client.ricette.toolbar.RichTextToolbar;
import net.lucamasini.ricette.client.util.DigitFilter;

import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.ui.client.ValueBoxEditorDecorator;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.*;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class RicetteDialog extends SimplePanel implements RicetteDialogView {

	private static RicetteDialogUiBinder uiBinder = GWT
			.create(RicetteDialogUiBinder.class);

	interface RicetteDialogUiBinder extends
			UiBinder<Widget, RicetteDialog> {
	}

	@UiField DialogBox dialog;
	
	@UiField TextBox titolo;
	@UiField SimplePanel toolbarContainer;
	@UiField RichTextArea descrizione;
	@UiField ValueBoxEditorDecorator<Integer> tempoPreparazione;
	@UiField IntegerBox difficolta;
	@UiField IntegerBox persone;
		
	@UiField Button ok;
	@UiField Button annulla;

	@UiField Button add;
	@UiField Button remove;

	@UiField CellTable<IngredienteProxy> ingredientiCellTableFrom;
	@UiField CellTable<IngredientiRicettaProxy> ingredientiCellTableTo;
	@UiField SimplePager pagerTo;
	@UiField SimplePager pagerFrom;
		
	private final SingleSelectionModel<IngredienteProxy> selectionModelIngredienti = new SingleSelectionModel<IngredienteProxy>();
	private final SingleSelectionModel<IngredientiRicettaProxy> selectionModelIngredientiRicetta = new SingleSelectionModel<IngredientiRicettaProxy>();
	
	private ListDataProvider<IngredienteProxy> fromDataProvider = new ListDataProvider<IngredienteProxy>();
	private ListDataProvider<IngredientiRicettaProxy> toDataProvider = new ListDataProvider<IngredientiRicettaProxy>();
	
	private Presenter presenter;
	
	public RicetteDialog() {
		add(uiBinder.createAndBindUi(this));

		fromDataProvider.addDataDisplay(ingredientiCellTableFrom);
		toDataProvider.addDataDisplay(ingredientiCellTableTo);
		
	    pagerFrom.setDisplay(ingredientiCellTableFrom);
	    pagerTo.setDisplay(ingredientiCellTableTo);
	    
	    RichTextToolbar toolbar = new RichTextToolbar(descrizione);
		toolbarContainer.setWidget(toolbar);
		
		//DigitFilter.applyTo(tempoPreparazione);
		DigitFilter.applyTo(difficolta);

	    ingredientiCellTableFrom.addColumn(
			new TextColumn<IngredienteProxy>(){
				@Override
				public String getValue(IngredienteProxy i) {
					return i.getDescrizione();
				}}, 
			"Ingrediente");

		ingredientiCellTableTo.addColumn(
			new TextColumn<IngredientiRicettaProxy>(){
				@Override
				public String getValue(IngredientiRicettaProxy ir) {
					return presenter.decodeIngrediente(ir);
				}}, 
			"Ingrediente");

		
        final Column<IngredientiRicettaProxy, String> quantita = new Column<IngredientiRicettaProxy, String>(
				new EditTextCell()) {
        	{
        		setFieldUpdater(new FieldUpdater<IngredientiRicettaProxy, String>() {

        			@Override
        			public void update(int index, IngredientiRicettaProxy ir, String value) {
    					ir.setQuantita(value);	
        			}
        		});
        	}
			@Override
			public String getValue(IngredientiRicettaProxy ir) {
				return ir.getQuantita();
			}
		};
		
		ingredientiCellTableTo.addColumn(
			quantita, 
			"Quantita");
		
		ingredientiCellTableFrom.setSelectionModel(selectionModelIngredienti);
		selectionModelIngredienti.addSelectionChangeHandler(new SelectionChangeEvent.Handler(){

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				if( selectionModelIngredienti.getSelectedObject()!=null) {
					add.setEnabled(true);
				} else {
					add.setEnabled(false);
				}
			}
		});
		
		ingredientiCellTableTo.setSelectionModel(selectionModelIngredientiRicetta);
		selectionModelIngredientiRicetta.addSelectionChangeHandler(new SelectionChangeEvent.Handler(){

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				if( selectionModelIngredientiRicetta.getSelectedObject()!=null) {
					remove.setEnabled(true);
				} else {
					remove.setEnabled(false);
				}
			}
		});
		
	}
	

	@UiHandler("ok")
	void onOk(ClickEvent event) {
		ok.setEnabled(false);
		presenter.salvaRicetta();
	}
	
	@UiHandler("annulla")
	void onCancella(ClickEvent event) {
		presenter.goTo(new RicettePlace());
		dialog.hide();
	}

	@UiHandler("add")
	void onAdd(ClickEvent event) {
		presenter.addIngrediente(selectionModelIngredienti.getSelectedObject());

	}

	@UiHandler("remove")
	void onRemove(ClickEvent event) {
		presenter.removeIngrediente(selectionModelIngredientiRicetta.getSelectedObject());
	}


	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}


	@Override
	public void showAndCenter() {
		dialog.center();
		dialog.show();
		ok.setEnabled(true);
	}


	@Override
	public void clearIngredientiSelection() {
		if( selectionModelIngredienti.getSelectedObject()!=null) {
			selectionModelIngredienti.setSelected(selectionModelIngredienti.getSelectedObject(), false);
		}
	}


	@Override
	public void clearIngredientiRicettaSelection() {
		if( selectionModelIngredientiRicetta.getSelectedObject()!=null) {
			selectionModelIngredientiRicetta.setSelected(selectionModelIngredientiRicetta.getSelectedObject(), false);
		}
	}


	@Override
	public void hide() {
		dialog.hide();
	}


	@Override
	public ListDataProvider<IngredienteProxy> fromDataProvider() {
		return fromDataProvider;
	}


	@Override
	public ListDataProvider<IngredientiRicettaProxy> toDataProvider() {
		return toDataProvider;
	}
}
